﻿using FastExecutor.Base.Annotation;
using FastExecutor.Base.Design;
using FastExecutor.Base.Model;
using FastExecutor.Base.Util;
using FastExecutor.ORG.Entity;
using FastExecutor.ORG.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace FastExecutor.ORG.Filter
{
    public class AuthFilter : ActionFilterAttribute
    {
        private AuthUtil @AuthUtil = InjectUtil.Container.Resolve<AuthUtil>();
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var Request = filterContext.RequestContext.HttpContext.Request;
            var Response = filterContext.RequestContext.HttpContext.Response;
            bool IsController = filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(FastAuthAttribute), true);
            bool IsAnonymous = @AuthUtil.CheckIsAnonymousAccess(Request.Url.ToString());
            if (IsController && !IsAnonymous)
            {
                //如果单个方法标记了允许匿名访问，这边不验证Token
                if (filterContext.ActionDescriptor.IsDefined(typeof(FastAllowAnonymousAttribute), true) || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(FastAllowAnonymousAttribute), true))
                {
                    base.OnActionExecuting(filterContext);
                }
                else
                {
                    if (@AuthUtil.ValidCookie(Request) && @AuthUtil.ValidToken(@AuthUtil.GetTokenString(Request)))
                    {
                        //刷新Cookie和Token的时间
                        @AuthUtil.RefreshAuth(Request, Response);
                        //BaseController基类参数赋值
                        string Token = @AuthUtil.GetTokenString(Request);
                        if (!string.IsNullOrEmpty(Token))
                        {
                            TokenEntity @TokenEntity = @AuthUtil.GetToken(Token);
                            BaseController controller = (BaseController)filterContext.Controller;
                            controller.UserGuid = @TokenEntity.UserGuid;
                            controller.DisplayName = @TokenEntity.DisplayName;
                            controller.LoginID = @TokenEntity.LoginID;
                            controller.Mobile = @TokenEntity.Mobile;
                            controller.EMail = @TokenEntity.EMail;
                            controller.OUGuid = @TokenEntity.OUGuid;
                            controller.OUName = @TokenEntity.OUName;
                            controller.OUCode = @TokenEntity.OUCode;
                            //如果是页面地址请求，判断模块权限
                            if (!filterContext.HttpContext.Request.IsAjaxRequest() && @AuthUtil.CheckIsModule(Request.Url.ToString()) && !@AuthUtil.CheckModuleRight(@TokenEntity.UserGuid, @TokenEntity.OUGuid, Request.Url.ToString()))
                            {
                                //跳转到无权限页面
                                filterContext.Result = new RedirectResult(CommonUtil.GetRootPath() + "Frame/Error/NoRight");
                            }
                        }
                        base.OnActionExecuting(filterContext);
                    }
                    else
                    {
                        filterContext.Result = new RedirectResult(CommonUtil.GetRootPath() + "Frame/Home/Login");
                    }
                }
            }
            else
            {
                if (@AuthUtil.ValidCookie(Request) && @AuthUtil.ValidToken(@AuthUtil.GetTokenString(Request)) && Request.Url.ToString().Contains("Frame/Home/Login"))
                {
                    //如果已登陆，访问登录页自动跳转主页
                    filterContext.Result = new RedirectResult(CommonUtil.GetRootPath() + System.Configuration.ConfigurationManager.AppSettings["DefaultHomeUrl"]);
                }
                else
                {
                    base.OnActionExecuting(filterContext);
                }

            }
            base.OnActionExecuting(filterContext);
        }
    }
}
